﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nox.Amuse.MVVM;
using System.Windows.Input;
using Nox.Amuse.View;
using System.Windows;

namespace Nox.Amuse.ViewModel
{
    public class CommandBarViewModel : ObservableObject
    {
        public CommandBarViewModel()
        {
            App.Messenger.Register<TabViewModel>(Messages.TabChanged, (tvm) => { UpdateCurrentTab(tvm); });
        }

        // VM to treat as model for current tab
        private TabViewModel myCurrentTab;

        private void UpdateCurrentTab(TabViewModel tvm)
        {
            myCurrentTab = tvm;
        }

        #region File Menu
        public ICommand ReconnectCommand
        {
            get
            {
                return myReconnectCommand ?? (myReconnectCommand = new RelayCommand(
                    () =>
                    {
                        myCurrentTab.Disconnect();
                        myCurrentTab.Connect();
                    },
                    () =>
                    {
                        if (myCurrentTab != null)
                            return true;
                        else return false;
                    }
                    ));
            }
        }
        private RelayCommand myReconnectCommand;

        public ICommand DisconnectCommand
        {
            get
            {
                return myDisconnectCommand ?? (myDisconnectCommand = new RelayCommand(
                    () => { myCurrentTab.Disconnect(); },
                    () =>
                    {
                        if (myCurrentTab != null)
                            return (myCurrentTab.SessionStatus != Model.SessionStatus.Disconnected ? true : false);
                        else return false;
                    }));
            }
        }
        private RelayCommand myDisconnectCommand;

        public ICommand CloseCommand
        {
            get
            {
                return myCloseCommand ?? (myCloseCommand = new RelayCommand(
                    () =>
                    {
                        if (System.Windows.MessageBox.Show(
                            "Close Tab?",
                            "Placeholder Alert",
                            System.Windows.MessageBoxButton.YesNo,
                            System.Windows.MessageBoxImage.Warning,
                            System.Windows.MessageBoxResult.No) == System.Windows.MessageBoxResult.Yes)
                        {
                            if (myCurrentTab.SessionStatus != Model.SessionStatus.Disconnected)
                                myCurrentTab.Disconnect();
                            myCurrentTab.Close();
                        }
                    },
                    () =>
                    {
                        if (myCurrentTab != null)
                            return true;
                        else return false;
                    }));
            }
        }
        private RelayCommand myCloseCommand;


        public ICommand OpenCommand
        {
            get
            {
                return myOpenCommand ?? (myOpenCommand = new RelayCommand(
                    () =>
                    {
                        var explorer = new SimpleWorldExplorer { DataContext = new WorldExplorerViewModel(), Owner = Application.Current.MainWindow };
                        explorer.ShowDialog();
                    }));
            }
        }
        private RelayCommand myOpenCommand;


        public ICommand TestConnectCommand
        {
            get
            {
                return myTestConnectCommand ?? (myTestConnectCommand = new RelayCommand(() =>
                { App.Messenger.NotifyColleagues(Messages.StartTestConnection); },
                () =>
                {
#if DEBUG
                    return true;
#else
                    return false;
#endif
                }
                ));
            }
        }
        private RelayCommand myTestConnectCommand;

        public ICommand ExitCommand
        {
            get
            {
                return myExitCommand ?? (myExitCommand = new RelayCommand(() =>
                    {
                        App.Messenger.NotifyColleagues(Messages.ExitRequest); // Display UI
                        App.Current.Shutdown(); // Attempt exit;
                    }));
            }
        }
        private RelayCommand myExitCommand;

        #endregion

        #region Help Menu
        public ICommand DisplayManualUpdateUI
        {
            get
            {
                return myDisplayManualUpdateUI ?? (myDisplayManualUpdateUI = new RelayCommand(() =>
                    {
                        var updateStatusDialog = new UpdateStatusDialog { DataContext = UpdateViewModel.CurrentUpdateViewModel, Owner = Application.Current.MainWindow };
                        updateStatusDialog.ShowDialog();
                        updateStatusDialog = null;
                    }));
            }
        }
        private RelayCommand myDisplayManualUpdateUI;

        public ICommand DisplayAboutBoxCommand
        {
            get
            {
                return myDisplayAboutBox ?? (myDisplayAboutBox = new RelayCommand(() =>
                    {
                        var model = new DialogHostDataModel(App.Current.MainWindow)
                        {
                            Content = new AboutBox(),
                            IconSource = new Uri("../Amuse.ico", UriKind.Relative),
                            Title = "About Amuse"
                        };
                        model.ShowDialog();
                    }));
            }
        }
        private RelayCommand myDisplayAboutBox;

        #endregion
    }
}
